﻿using System;
using Orchard.DisplayManagement;
using Orchard.Forms.Services;
using Orchard.Localization;

namespace Contrib.Projections.Providers.Layouts {

    public class SeparatorLayoutForms : IFormProvider {
        protected dynamic Shape { get; set; }
        public Localizer T { get; set; }

        public SeparatorLayoutForms(
            IShapeFactory shapeFactory) {
            Shape = shapeFactory;
            T = NullLocalizer.Instance;
        }

        public void Describe(DescribeContext context) {
            Func<IShapeFactory, object> form =
                shape => {
                    var f = Shape.Form(
                        Id: "SeparatorLayout",
                        _Prperties: Shape.Fieldset(
                            Title: T("Separator"), 
                            _Separator: Shape.TextBox(
                                Id: "separator", Name: "Separator",
                                Title: T("Separator"),
                                Description: T("The separator to provide."),
                                Classes: new[] { "textMedium", "tokenized" }
                                )
                            )
                        );

                    return f;
                };

            context.Form("SeparatorLayout", form);
        }
    }

    public class SeparatorLayoutFormsValitator : FormHandler {
        public Localizer T { get; set; }

        public override void Validating(ValidatingContext context) {
            if (context.FormName == "SeparatorLayout") {
                if (context.ValueProvider.GetValue("Separator") == null) {
                    context.ModelState.AddModelError("Separator", T("You must provide a Separator").Text);
                }
            }
        }
    }

}